package tjs.study.mini.springbootstartsmini.autoconfigure.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import tjs.study.mini.springbootstartsmini.autoconfigure.condition.ConditionalOnClass;

/**
 * @author nicky
 * @date 2021/9/9 下午12:32
 */
@Configuration
@ConditionalOnClass({DispatcherServlet.class, ConfigurableWebApplicationContext.class})
public class DispatcherServletAutoConfiguration {

    public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";

    @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    public DispatcherServlet dispatcherServlet() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        return dispatcherServlet;
    }

    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet) {
        DispatcherServletRegistrationBean dispatcherServletRegistrationBean = new DispatcherServletRegistrationBean(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME, dispatcherServlet);
        return dispatcherServletRegistrationBean;
    }
}
